import time
import vec

WIDTH = 250
HEIGHT = 250

BACK_COLOR = (0, 0, 127)
CIRCLE_COLOR = (0, 200, 0)

lastTime = time.time()

class MyCircle:
    def __init__(self, pos, r):
        self.pos = pos
        self.r = r

    def Draw(self):
        screen.draw.filled_circle((self.pos.x, self.pos.y), self.r, CIRCLE_COLOR)


def on_key_down(key):
    if key == keys.ESCAPE:
        quit()

def on_mouse_down(pos):
    print("mouse_down", pos)

def on_mouse_up(pos):
    print("mouse_up", pos)

start = vec.Vec2(50,50)
target = vec.Vec2(50,50)
full_length = (target - start).length()
c = MyCircle(start, 30)
spd = 30
road_dir = target - c.pos
road_dir.normalize()
is_moving = True
move_dir = target - start
move_dir.normalize()
num=1
def move(sta,en):
    global start,target,full_length,c,spd,road_dir,is_moving,move_dir
    start = sta
    target = en
    full_length = (target - start).length()
    road_dir = target - c.pos
    road_dir.normalize()
    is_moving = True
    move_dir = target - start
    move_dir.normalize()

#move(vec.Vec2(50,50),vec.Vec2(50,200))

def update():
    global is_moving, lastTime,num
    if is_moving == False:
        return

    # move_dir = target - c.pos
    moved_length = (c.pos - start).length()

    curTime = time.time()
    dt = curTime - lastTime
    lastTime = curTime
    # print(dt)
    if moved_length < full_length:
        c.pos = c.pos + move_dir * spd * dt
    else:
        if(num==1):
            c.pos=target
            move(vec.Vec2(50,50),vec.Vec2(50,200))
        elif(num==2):
            c.pos=target
            move(vec.Vec2(50,200),vec.Vec2(200,200))
        elif(num==3):
            c.pos=target
            move(vec.Vec2(200,200),vec.Vec2(200,50))
        elif(num==4):
            c.pos=target
            move(vec.Vec2(200,50),vec.Vec2(50,50))
        else:
            c.pos=target
            num=0
        num+=1
def draw():
    screen.fill(BACK_COLOR)
    c.Draw()